<?php


namespace App\Http\Controllers\Api;

use App\Services\Member\Models\Role;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Constant\ApiV2Constant;
use App\Services\Business\Models\Appraise;
use App\Services\Member\Services\RoleService;
use App\Services\Member\Interfaces\RoleServiceInterface;
use App\Services\Member\Models\UserRole;


class RoleController extends BaseController
{

    /**
     * @var RoleService
     */
    protected $roleService;


    public function __construct(
        RoleServiceInterface $roleService

    )
    {
        $this->roleService = $roleService;

    }


    public function index(Request $request)
    {

        $companyId = $request->input('company_id');
        $roles =   Role::query()->where('company_id',$companyId)->get()->toArray();

        return $this->successData(compact('roles'));
    }

    /**
     * @api {get} /api/v2/role/{id} VIP会员详情
     * @apiGroup VIP
     * @apiVersion v2.0.0
     *
     * @apiSuccess {Number} code 0成功,非0失败
     * @apiSuccess {Object} data 数据
     * @apiSuccess {Number} data.id VIPid
     * @apiSuccess {String} data.name VIP名
     * @apiSuccess {Number} data.charge 价格
     * @apiSuccess {Number} data.expire_days 有效天数
     * @apiSuccess {String[]} data.desc_rows 描述
     */
    public function detail($id)
    {
        $role = $this->roleService->get($id);
        return $this->successData(compact('role'));
    }


    /**
     * 会员所有的评价
     * @param Request $request
     * @return JsonResponse
     */
    public function appraise(Request $request)
    {
        $companyId =$request->input('company_id');
        $size = $request->input('size', 20);
        $userIds = UserRole::query()->where('company_id', $companyId)->pluck('user_id')->toArray();
        $appraises = Appraise::query()
            ->with(['user:id,avatar,nick_name'])
            ->where('company_id', $companyId)
            ->whereIn('user_id', $userIds)
            ->paginate($size);
        return $this->successData(compact('appraises'));

    }


}
